﻿using Microsoft.Lync.Model;
using Microsoft.Lync.Model.Group;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace LyncSilverlightApplication
{
    public partial class conversation : ChildWindow
    {
        #region "グローバル変数"

        public Dictionary<string, string> users = new Dictionary<string, string>();
        LyncClient LyncClient;
        ContactManager ContactManager;
        public ObservableCollection<GroupInfo> Groups { get; set; }
        #endregion

        #region "フォームロードイベント"
        public conversation()
        {
            InitializeComponent();
        }

        private void ChildWindow_Loaded_1(object sender, RoutedEventArgs e)
        {
            try
            {
                Groups = new ObservableCollection<GroupInfo>();
                LyncClient = LyncClient.GetClient();
                ContactManager = LyncClient.ContactManager;
                this.DataContext = this;
                PopulateGroupInfo();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region "プライベート機能"
        private void PopulateGroupInfo()
        {
            string displayname = string.Empty;

            //Lyncクライアントの連絡先情報取得
            foreach (var group in LyncClient.ContactManager.Groups)
            {
                foreach (var contact in group)
                {
                    if (group.Type != GroupType.FrequentContacts)
                    {
                        displayname = contact.GetContactInformation(ContactInformationType.DisplayName).ToString();

                        if (users.ContainsKey(displayname))
                            continue;

                        users.Add(displayname, contact.Uri);
                        displayname = string.Empty;
                    }

                }
            }
            foreach (KeyValuePair<string, string> pair in users)
            {
                ButtonItem buttonItem = new ButtonItem();
                buttonItem.SourceUri = pair.Value;
                buttonItem.DisplayName = pair.Key;
                contactListBox.Items.Add(buttonItem);
            }
        }
        #endregion

        #region "ボタンイベント"
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        #endregion
    }
}

